<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * 用户注册 控制器
 */
class Register extends Front_Controller {

    public function __construct() {
        parent::__construct();

        //载入用户验证服务
        $this->load->service('user/user_auth_service', 'auth');

        //如果关闭注册
        if (!$this->config->item('enable_register', 'auth') && METHOD_NAME != 'close') {
            redirect('register/close');
        }

        log_message('debug', 'Register Controller Class Initialized');
    }

    /**
     * 注册显示页面
     */
    public function index() {
        if (($result = $this->auth->check_default_group()) instanceof Error) {
            $this->show_error($result->get_error());
        }
        if (($result = $this->auth->check_ip()) instanceof Error) {
            $this->show_error($result->get_error());
        }

        $data = array();
        $data['seo']['title'] = '用户注册';
        $this->load->view('register/register_index', $data);
    }

    /**
     * 注册提交处理
     */
    public function doindex() {
        $username = $this->input->post('username', true);
        $password = $this->input->post('password', true);
        $email = $this->input->post('email', true);

        //如果注册出错
        if (($info = $this->auth->register($username, $password, $email)) instanceof Error) {
            $this->show_error($info->get_error());
        } else {
            //创建用户注册标志
            $identity = $this->auth->create_regist_identify($info['uid'], $info['password']);
            //如果注册需要邮件激活
            if ($this->config->item('email_activation', 'auth')) {
                redirect('register/send_active_email?statu=' . $identity);
            } else {
                redirect('register/welcome?statu=' . $identity);
            }
        }
    }

    /**
     * 完成注册后的欢迎页面
     * 同步登陆
     */
    public function welcome() {
        if (!$this->input->get('statu', true)) redirect('register');
        $statu = $this->_check_register_user();
        if (Utility::get_status($this->user->info['status'], 2)) {
            redirect('register/send_active_email?statu=' . $statu);
        }

        //开始登陆
        $this->auth->set_login_cookie($this->user->info, true);

        //登陆后跳转到注册完成引导页
        redirect('register/guide');
    }

    /**
     * 注册完成后引导页面
     */
    public function guide() {
        //如果未登录则跳转 redirect('register');
        $next = false;
        //$key = $this->input->input('key', true);
        //$this->load->service('user/user_register_guide_service');
        //$next = $this->user_register_guide_service->get_next_guide($key);
        if ($next) {
            redirect($next['guide']);
        } else {
            $this->load->library('uc/uc_api');
            $data = array();
            $data['username'] = $this->user->username;
            $data['type'] = 'success';
            $data['synlogin'] = $this->uc_api->user_synlogin($this->user->uid); //获取同步登陆js
            $this->load->view('register/register_about', $data);
        }
    }

    /**
     * 关闭注册页面
     */
    public function close() {
        //如果开启注册
        if ($this->config->item('enable_register', 'auth')) {
            redirect('register');
        }

        $data = array();
        $data['seo']['title'] = '用户注册';
        $data['close_reason'] = $this->config->item('close_reason', 'auth');
        $this->load->view('register/register_close', $data);
    }

    /**
     * 发送激活邮件
     */
    public function send_active_email() {
        $statu = $this->_check_register_user();
        $data = array();
        //检查用户状态是否已经激活
        $active_email = true;
        if ($active_email) {
            $data['type'] = 'active_email';
            $this->load->view('register/register_about', $data);
        } else {
            //检查是否已经发送了激活邮箱
            $send_activation_email = true;
            if (!$send_activation_email) {
                $info = array();
                $this->auth->send_activation_email($info['username'], $info['email'], $statu, $info['uid']);
            }

            $mail_list = array('gmail.com' => 'google.com');
            list(, $mail) = explode('@', $info['email'], 2);
            $goto_email = 'http://mail.' . (isset($mail_list[$mail]) ? $mail_list[$mail] : $mail);

            $data['email'] = $info['email'];
            $data['username'] = $info['username'];
            $data['goto_email'] = $goto_email;
            $data['statu'] = $statu;
            $data['from'] = '';
            $this->load->view('register/register_emailactive', $data);
        }
    }

    /**
     * 再次发送激活邮件
     *
     * 用户如果没有收到邮箱,点击该链接,再次发发送激活邮箱
     */
    public function send_active_email_again() {

    }

    /**
     * 更改激活邮箱操作
     *
     * 用户未激活前想更改邮箱，则post提交到这里
     */
    public function edit_active_email() {

    }

    /**
     * 激活邮箱链接
     *
     * 用户收到激活邮件后，点击的激活链接地址
     */
    public function active_email() {

    }

    /**
     * 验证用户名
     *
     */
    public function check_username() {
        $username = $this->input->post('username', true);

        if (($error = $this->auth->check_username($username)) instanceof Error) {
            $this->show_error($error->get_error());
        }
        $this->show_success();
    }

    /**
     * 验证密码复杂度是否符合
     */
    public function check_password() {
        $username = $this->input->post('username', true);
        $password = $this->input->post('password', true);
        if (($error = $this->auth->check_password($password, $username)) instanceof Error) {
            $this->show_error($error->get_error());
        }
        $this->show_success();
    }

    /**
     * 验证密码强度
     */
    public function check_password_strong() {
        $password = $this->input->post('password', true);
        $rank = $this->auth->check_password_strong($password);
        $this->show_success($rank);
    }

    /**
     * 验证Email
     */
    public function check_email() {
        $email = $this->input->post('email', true);
        if (($error = $this->auth->check_email($email)) instanceof Error) {
            $this->show_error($error->get_error());
        }
        $this->show_success();
    }

    /**
     * 验证手机号码
     */
    public function check_mobile() {

    }

    /**
     * 验证验证码
     */
    public function check_captcha() {

    }

    /**
     * 验证用户注册标志
     */
    protected function _check_register_user() {
        //该get获取得到的内容会被过滤，这里再次转换下
        $statu = $this->input->get('statu', true);
        $identify = rawurlencode($statu);
        //$code = $this->auth->create_regist_identify(29, '7fbe1fedd3fa5b9620c882890315d847');
        //var_dump($statu, $identify, $code);
        if (!$identify) $this->show_error($this->error->set_error('AUTH:illegal_request')->get_error());

        list($uid, $password) = $this->auth->parser_regist_identify($identify);
        $user = $this->db->get_where('user', array('uid' => $uid))->row_array();
        if (Utility::get_pwd_code($user['password']) != $password) {
            $this->show_error('AUTH:illegal_request');
        }

        $this->load->service('user/bo/user_bo');
        $this->user = $this->user_bo->init($uid);

        return $identify;
    }
}

/* End of file register.php */
/* Location: ./application/controllers/register.php */
